Skip to content

allow "spotlessFiles" project property in gradle plugin to only target specific files #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class SpotlessPlugin implements Plugin<Project> {
private static final String TASK_GROUP = "Verification";
private static final String CHECK_DESCRIPTION = "Checks that sourcecode satisfies formatting steps.";
private static final String APPLY_DESCRIPTION = "Applies code formatting steps to sourcecode in-place.";
private static final String FILES_PROPERTY = "spotlessFiles";

@Override
public void apply(Project project) {
Expand All @@ -63,12 +64,20 @@ void createTasks(Project project) {
Task rootApplyTask = project.task(EXTENSION + APPLY);
rootApplyTask.setGroup(TASK_GROUP);
rootApplyTask.setDescription(APPLY_DESCRIPTION);
String filePatterns;
if (project.hasProperty(FILES_PROPERTY) && project.property(FILES_PROPERTY) instanceof String) {
filePatterns = (String) project.property(FILES_PROPERTY);
} else {
// needs to be non-null since it is an @Input property of the task
filePatterns = "";
}

spotlessExtension.formats.forEach((key, value) -> {
// create the task that does the work
String taskName = EXTENSION + capitalize(key);
SpotlessTask spotlessTask = project.getTasks().create(taskName, SpotlessTask.class);
value.setupTask(spotlessTask);
spotlessTask.setFilePatterns(filePatterns);

// create the check and apply control tasks
Task checkTask = project.getTasks().create(taskName + CHECK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -52,8 +51,6 @@
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;

public class SpotlessTask extends DefaultTask {
private static final String FILES_PROPERTY = "spotlessFiles";

// set by SpotlessExtension, but possibly overridden by FormatExtension
protected String encoding = "UTF-8";

Expand Down Expand Up @@ -89,6 +86,17 @@ public void setPaddedCell(boolean paddedCell) {
this.paddedCell = paddedCell;
}

protected String filePatterns = "";

@Input
public String getFilePatterns() {
return filePatterns;
}

public void setFilePatterns(String filePatterns) {
this.filePatterns = Objects.requireNonNull(filePatterns);
}

protected FormatExceptionPolicy exceptionPolicy = new FormatExceptionPolicyStrict();

public void setExceptionPolicy(FormatExceptionPolicy exceptionPolicy) {
Expand Down Expand Up @@ -177,10 +185,8 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
.build()) {
// determine if a list of files has been passed in
Predicate<File> shouldInclude;
Project project = getProject();
if (project.hasProperty(FILES_PROPERTY) && project.property(FILES_PROPERTY) instanceof String) {
Object rawIncludePatterns = project.property(FILES_PROPERTY);
final String[] includePatterns = ((String) rawIncludePatterns).split(",");
if (this.filePatterns != null && !this.filePatterns.isEmpty()) {
final String[] includePatterns = this.filePatterns.split(",");
final List<Pattern> compiledIncludePatterns = Arrays.stream(includePatterns)
.map(Pattern::compile)
.collect(Collectors.toList());
Expand All @@ -195,7 +201,7 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
List<File> outOfDate = new ArrayList<>();
inputs.outOfDate(inputDetails -> {
File file = inputDetails.getFile();
if (file.isFile() && !file.equals(getCacheFile()) && shouldInclude.test(file)) {
if (shouldInclude.test(file) && file.isFile() && !file.equals(getCacheFile())) {
outOfDate.add(file);
}
});
Expand All @@ -206,12 +212,17 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
if (getCacheFile().exists()) {
LastApply lastApply = SerializableMisc.fromFile(LastApply.class, getCacheFile());
for (File file : lastApply.changedFiles) {
if (!outOfDate.contains(file) && file.exists() && Iterables.contains(target, file) && shouldInclude.test(file)) {
if (shouldInclude.test(file) && !outOfDate.contains(file) && file.exists() && Iterables.contains(target, file)) {
outOfDate.add(file);
}
}
}

if (outOfDate.isEmpty()) {
// no work to do
return;
}

if (apply) {
List<File> changedFiles = applyAnyChanged(formatter, outOfDate);
if (!changedFiles.isEmpty()) {
Expand Down